<template>
  <div class="rightBodyContent">
    <div class="rightBodyContent-in">
      <div class="mb-10">
        <div class="toplineBox">
          <div class="toplineBox-left">
            <el-button size="small" @click="addStudent()" type="primary"><i class="el-icon-plus"></i>添加年段</el-button>
          </div>
          <p class="numberline toplineBox-right">总{{ page.total }}条</p>
        </div>
      </div>
      <!--本体列表，数据 -->
      <div class="table-body">
        <div class="table-body-box">
          <el-table
              ref="multipleTable"
              :data="arryList"
              :stripe="tableStyle.tableStripe"
              :border="tableStyle.tableBorder"
              tooltip-effect="dark"
              style="width: 100%"
              header-cell-class-name="table-text-c"
              cell-class-name="table-text-c"
          >
            <el-table-column
                prop="id"
                label="ID"
                width="80">
            </el-table-column>
            <el-table-column
                prop="section_name"
                label="年段">
            </el-table-column>
            <el-table-column
                label="升学" width="150">
              <template slot-scope="scope">
                <span v-if="scope.row.type==0">无</span>
                <span v-if="scope.row.type==1">{{ scope.row.upgrade ? scope.row.upgrade.section_name : '' }}</span>
                <span v-if="scope.row.type==2">毕业</span>
              </template>
            </el-table-column>
            <el-table-column
                label="上次操作时间">
              <template slot-scope="scope">
                {{ scope.row.update_time ? scope.row.update_time : '--' }}
              </template>
            </el-table-column>
            <el-table-column
                label="操作"
                width="250">
              <template slot-scope="scope">
                <el-button size="small" @click="addStudent(scope.row)">详情</el-button>
                <el-button size="small" @click="detelFn(scope.row.id,scope.$index)">删除</el-button>
                <el-button type="primary" size="small" v-if="scope.row.type==1" @click="upgrade(scope.$index)">升学
                </el-button>
                <el-button type="primary" size="small" v-if="scope.row.type==2" @click="graduation(scope.$index)">毕业
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <!--工具条 分页-->
      <el-col :span="24" class="toolbar">
        <el-pagination
            background
            layout="total, prev, pager, next"
            :total="page.total"
            :page-size="page.pagesize"
            @current-change="handleSizeChange">
        </el-pagination>
      </el-col>
    </div>
    <addSegment :visible.sync="addSegmentVisible" :editId="editId" @refreshFn="refreshFn"></addSegment>
    <upload-class :visible.sync="uploadclassVisible" @refreshFn="refreshFn" :segmentId="segmentId"
                  :upgradeId="upgrade_id"></upload-class>
    <graduation :visible.sync="graduationVisible" @refreshFn="refreshFn" :segmentId="segmentId"
                :section_name="section_name"></graduation>
  </div>
</template>

<script>
import addSegment from '@/components/dialog/addSegment.vue'
import {pagination, tableoption} from '@/utils/util.js'
import UploadClass from '@/components/dialog/uploadClass'
import graduation from '@/components/dialog/graduation'

export default {
  components: {
    UploadClass,
    addSegment,
    graduation
  },
  data() {
    return {
      addSegmentVisible: false,
      uploadclassVisible: false,
      graduationVisible: false,
      editId: '',
      arryList: [],
      handleSelection: [],
      // 设置table样式
      tableStyle: tableoption,
      // 设置table页面分页所需
      page: {
        pageindex: pagination.pageIndex,
        pagesize: pagination.pageSize,
        total: pagination.total
      },
      section_id: '',
      sectionList: [{
        value: '无',
        label: '无'
      }, {
        value: '升学',
        label: '升学'
      }, {
        value: '毕业',
        label: '毕业'
      }],
      segmentId: 0,
      upgrade_id: 0,
      section_name: ''
    }
  },
  created() {
    this.InitFn()
  },
  methods: {
    // 初始化
    InitFn() {
      this.$post(this.api.admin_section_index, {
        p: this.page.pageindex
      }).then((response) => {
        if (response.code === 0) {
          this.arryList = response.list.data
          this.page.total = response.list.total
        } else {
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    checkIdCard() {
      this.changeCartVisible = true
    },
    handleSelectionChange(val) {
      this.handleSelection = val
    },
    // 分页
    handleSizeChange(val) {
      this.page.pageindex = val
      this.InitFn()
    },
    // 刷新页面
    refreshFn() {
      this.InitFn()
    },
    addStudent(option) {
      let id = option || ''
      // this.$router.push('roleDetail?id='+id)
      this.editId = option || ''
      this.addSegmentVisible = true
    },
    detelFn(id, index) {
      let self_ = this
      this.$confirm('此年段将永久删除，该年段下的学生将不在对应，确认删除此年段？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        self_.$post(this.api.admin_section_detel, {
          id: id
        }).then((response) => {
          if (response.code === 0) {
            this.arryList.splice(index, 1)
            this.$message({
              message: '删除成功',
              type: 'success'
            })
          } else {
            this.$message.error(response.msg)
          }
        }).catch((err) => {
          console.log(err)
        })
      }).catch(() => {

      })
    },
    // 升学
    upgrade(option) {
      let classLine = this.arryList[option]
      this.segmentId = classLine.id
      this.upgrade_id = classLine.upgrade_id
      this.uploadclassVisible = true
    },
    // 毕业
    graduation(option) {
      this.segmentId = this.arryList[option].id
      this.section_name = this.arryList[option].section_name
      this.graduationVisible = true
    }
  },
  mounted() {
  }
}
</script>
